import Vue from 'vue'
const router = {
	push: ({
		path,
		query
	}) => {
		if (path === '/pages/webView/main' && !query.type) {
			path = '/pages/external/main'
		}

		// if (path === '/pages/webView/main') {
		//   path = '/pages/external/main'
		// }
		if (query) {
			if (query.type) {
				delete query.type
			}
			var queryUrl
			if (path === '/pages/webView/main') {
				queryUrl = encodeURIComponent('?' + paramUrl(query))
			} else {
				queryUrl = paramUrl(query)
			}
			var url = '/pages' + path + '/index' + '?' + queryUrl
			console.log(url,'浏览量')
		} else {
			url = '/pages' + path + '/index'
		}
		uni.navigateTo({
			url: url
		})
	},
	switchTab: ({
		path,
		query
	}) => {
		let queryUrl = paramUrl(query)
		let url = path + '?' + queryUrl
		uni.switchTab({
			url: url
		})
	},
	redirectTo: ({
		path,
		query
	}) => {
		var url
		if (query) {
			var queryUrl = paramUrl(query)
			url = path + '?' + queryUrl
		} else {
			url = path
		}

		uni.redirectTo({
			url: url
		})
	}
}

const route = function() {
	console.log(this.$mp.page.options,'大大')
	return this.$mp.page.options
}

function paramUrl(data) {
	let url = ''
	for (var k in data) {
		let value = data[k] !== undefined ? data[k] : ''
		url += '&' + k + '=' + value
	}
	return url ? url.substring(1) : ''
}

const localStorage = {
	getItem(key) {
		return uni.getStorageSync(key)
	},
	setItem(key, data) {
		uni.setStorage({
			key: key,
			data: data
		})
	},
	removeItem(key) {
		uni.removeStorageSync(key)
	}
}
const message = ({
	message
}) => {
	uni.showToast({
		title: message,
		icon: 'none'
	})
}
Vue.prototype.$uniRouter = router
Vue.prototype.$uniRoute = route
Vue.prototype.$localStorage = localStorage
Vue.prototype.$sessionStorage = localStorage
Vue.prototype.$message = message
